package Bench::Test::View::HTML;

use 5.008004;
use strict;
use warnings;

use Template;

sub process {
   my $data = shift;
   my $output;

   my $config = {
      INTERPOLATE => 1,  # expand "$var" in plain text
      POST_CHOMP  => 0,  # cleanup whitespace
      PRE_CHOMP   => 0,
      EVAL_PERL   => 1,  # evaluate Perl code blocks
   };

   # create Template object
   my $template = Template->new($config);

   # define template variables for replacement
   my $vars = {
      data => $data,
   };

   # process input template, substituting variables
   $template->process(\*DATA, $vars, \$output)
     || die $template->error();

   return $output;
}


1;

# Template for HTML output

__DATA__
[%- PERL -%]
   use Data::Dumper;
   my $data   = $stash->get('data');
   my $header = shift @{$data};
   $stash->set('header', $header);
   $stash->set('rowtype', 'rowB');
[%- END -%]
<html>
   <head>
      <style type="text/css">
         table {
            text-align: center;
            font-family: tahoma, sans-serif;
            font-size: 0.7em;
            color: #777;
            margin-bottom: 1.5em;
         }
         table th {
            font-weight: bold;
            padding: 0.5em;
            color: #444;
            background-color: #d0d0d0;
         }
         table td {
            padding: 0.5em;
         }
         table tr.rowA {
            background-color: #efefef;
         }
         table tr.rowB {
            background-color: #fafafa;
         }
      </style>
   </head>
   <body>
      <table>
         <tr>
         [%- FOREACH row IN header %]
            <th>$row</th>
         [%- END %]
         </tr>
         [%- FOREACH row IN data -%]
            [%- IF rowtype == 'rowA' -%]
               [%- rowtype = 'rowB' -%]
            [%- ELSE -%]
               [%- rowtype = 'rowA' -%]
         [%- END %]
         <tr class=$rowtype>
            [%- FOREACH item IN row %]
            <td>$item</td>
            [%- END %]
         </tr>
         [%- END %]
      </table>
   </body>
</html>
